package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.ActivityRegisterDao;
import cn.topevery.garden.entity.po.ActivityRegister;
import cn.topevery.garden.entity.query.ActivityRegisterQuery;
import cn.topevery.garden.entity.vo.ActivityRegisterVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 公园大型活动登记
 *
 * @author code-generate
 * @date 2022-10-28
 */
@Repository
public class ActivityRegisterDaoImpl extends BaseDaoImpl<ActivityRegister> implements ActivityRegisterDao {
    @Override
    public <P extends DbCsp> List<ActivityRegister> getSearch(P para) {
        return null;
    }


    @Override
    public DataPager<ActivityRegisterVO> listPage(ActivityRegisterQuery query) {
        String table = "t_activity_register tar \n" +
                "left join t_sys_dict tsd on tsd.c_dict_id=tar.c_type_id\n" +
                "left join t_park tp on tp.c_id=tar.c_park_id";

        String columns = "tar.*,tsd.c_dict_name typeName,tp.c_name parkName";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tar.c_id desc");
        sqlQueryMate
                .where(true, "tar.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(query.getCode()), "tar.c_code", ScriptConditionEnum.Like, query.getCode())
                .where(StringUtils.isNotBlank(query.getName()), "tar.c_name", ScriptConditionEnum.Like, query.getName())
                .where(query.getParkId() != null, "tar.c_park_id", ScriptConditionEnum.Eq, query.getParkId())
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, ActivityRegisterVO.class);
    }

    @Override
    public ActivityRegister getEntityByColumn(String key, String value) {
        KeyValue<String> map = new KeyValue<>();
        map.key = key;
        map.value = value;
        return getEntityByColumnEqValue(map);
    }

    @Override
    public DataPager<ActivityRegisterVO> countActivities(ActivityRegisterQuery query) {
        String table = "(SELECT\n" +
                "   ELT( INTERVAL ( tar.c_number, 1, 100, 500, 1000 ), '1-100', '100-500', '500-1000', '1000+' ) AS region,\n" +
                "   YEAR ( tar.c_begin_date ) AS years,\n" +
                "   QUARTER ( tar.c_begin_date ) season,\n" +
                "   MONTH ( tar.c_begin_date ) AS months,\n" +
                "   tar.c_park_id parkId,\n" +
                "   tp.c_name parkName,\n" +
                "   tp.c_distract_id distractId,\n" +
                "   tp.c_distract_name distractName,\n" +
                "   tar.c_type_id typeId,\n" +
                "   tsd.c_dict_name typeName,\n" +
                "   count(*) count \n" +
                " FROM\n" +
                "   `t_activity_register` tar\n" +
                "   LEFT JOIN t_park tp ON tp.c_id = tar.c_park_id\n" +
                "   LEFT JOIN t_sys_dict tsd ON tsd.c_dict_id = tar.c_type_id \n" +
                " WHERE\n" +
                "   tar.c_db_status = 0 \n" +
                " GROUP BY\n" +
                "   region,\n" +
                "   years,\n" +
                "   season,\n" +
                "   months,\n" +
                "   tar.c_park_id,\n" +
                "   tp.c_name,\n" +
                "   tp.c_distract_id,\n" +
                "   tp.c_distract_name,\n" +
                "   tar.c_type_id,\n" +
                "   tsd.c_dict_name \n" +
                " ORDER BY\n" +
                "   tar.c_park_id,\n" +
                "   months ) t";
        String columns = "*";
        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate.where(query.getDistractId() != null, "t.distractId", ScriptConditionEnum.Eq, query.getDistractId())
                .where(query.getParkId() != null, "t.parkId", ScriptConditionEnum.Eq, query.getParkId())
                .where(StringUtils.isNotBlank(query.getTypeId()), "t.typeId", ScriptConditionEnum.Eq, query.getTypeId())
                .where(query.getYears() != null, "t.years", ScriptConditionEnum.Eq, query.getYears())
                .where(query.getMonths() != null, "t.months", ScriptConditionEnum.Eq, query.getMonths());
        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;
        return getDataPager(sqlQueryMate, pageSize, pageIndex, ActivityRegisterVO.class);
    }
}
